package com.xjn.updatetool.util;


import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * @author lilin
 * @create 2020-07-29 10:58
 **/
public class FileNameUtil {
    /**
     * 根据源目录，变换到目标目录
     * @param path
     * @param src
     * @param dst
     * @return
     * @throws Exception
     */
    public static String changeBaseDir(String path,String src,String dst) throws Exception{
        if(StringUtils.isBlank(path) || StringUtils.isBlank(src) || StringUtils.isBlank(dst)){
            return "";
        }
        if(path.startsWith(src)) {
            return path.replace(src, dst);
        }else{
            return "";
        }
    }

    public static String createBakFilePath(File file,LocalDateTime nowTime) throws Exception{
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(file.getParent())
                .append(File.separator)
                .append(file.getName())
                .append(nowTime.getYear())
                .append(MyStringUtils.getCodeByLength(2,""+nowTime.getMonthValue()))
                .append(nowTime.getDayOfMonth())
                .append(nowTime.getHour())
                .append(nowTime.getMinute())
                .append(nowTime.getSecond())
                .append("bak");
        return stringBuffer.toString();
    }

    public static String createLogFilePath(String dir,LocalDateTime nowTime,String tag) throws Exception{
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(dir)
                .append(File.separator)
                .append(tag)
                .append(nowTime.getYear())
                .append(MyStringUtils.getCodeByLength(2,""+nowTime.getMonthValue()))
                .append(nowTime.getDayOfMonth())
                .append(nowTime.getHour())
                .append(nowTime.getMinute())
                .append(nowTime.getSecond())
                .append(".txt");
        return stringBuffer.toString();
    }
}
